<?php

declare(strict_types=1);

namespace Modules\Member\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Illuminate\Http\Request;
use Modules\Member\Models\Members;
use Modules\Member\Models\MembersPriceLog;
use Modules\Order\Models\Orders;

class MembersPriceLogController extends Controller
{
    public function __construct(
        protected readonly MembersPriceLog $model
    ) {
    }

    public function index(): mixed
    {
        $list =  $this->model->setBeforeGetList(function ($query){
            return $query->with('user');
        })->getList()->toArray();
        foreach ($list['data'] as $k=>$v){
            $orderInfo = Orders::where('order_sn',$v['order_sn'])->first();
            if(!empty($orderInfo)){
                $list['data'][$k]['from_user'] = Members::select(['id','avatar','username','mobile','user_level'])->find($orderInfo->user_id)->toArray();
            }elseif(!empty($list['data'][$k]['start_user_id'])){
                $list['data'][$k]['from_user'] = Members::select(['id','avatar','username','mobile','user_level'])->find($list['data'][$k]['start_user_id'])->toArray();;
            }else{
                $list['data'][$k]['from_user'] = [];
            }

                $list['data'][$k]['created_at'] = $this->model->where('order_sn',$v['order_sn'])->value('created_at')->toDateTimeString();
            

        }
        return $list;
    }

    /**
     * @return mixed
     */
    public function store(Request $request)
    {
        return $this->model->storeBy($request->all());
    }

    /**
     * @return mixed
     */
    public function show($id)
    {
        return $this->model->firstBy($id);
    }

    /**
     * @return mixed
     */
    public function update($id, Request $request)
    {
        return $this->model->updateBy($id, $request->all());
    }

    /**
     * @return mixed
     */
    public function destroy($id)
    {
        return $this->model->deleteBy($id);
    }

    /**
     * @return bool
     */
    public function enable($id)
    {
        return $this->model->toggleBy($id);
    }
}
